<template>
    <div id="notice" :class="{ 'close-tab-second': isExit3 }">
        <el-tabs type="card" v-model="activeKey" closable @tab-remove="removeTab">
            <el-tab-pane name="触警邮件推送规则设置">
                <span slot="label">
                    <i class="el-icon-tickets"></i>
                    触警邮件推送规则设置
                </span>
                <WarningNoticeList v-if="activeKey === '触警邮件推送规则设置'"></WarningNoticeList>
            </el-tab-pane>
            <el-tab-pane name="邮件发送管理" v-if="isExit3">
                <span slot="label">
                    <i class="el-icon-tickets"></i>
                    邮件发送管理
                </span>
                <EmailSendManage :activeValue="activeKey" @look-detail="changComponets"></EmailSendManage>
            </el-tab-pane>
            <el-tab-pane v-for="item in tabGroup" :key="item.name" :label="item.name" :name="item.name">
                <!-- 风险监控屏 -->
                <WarningNoticeDetail v-if="['add', 'look', 'edit'].includes(item.type)" :data="item" @close-tab="removeTab"></WarningNoticeDetail>
                <!-- 静态风控 -->
                <WarningNoticeDetailStatic
                    v-if="['addStatic', 'lookStatic', 'editStatic'].includes(item.type)"
                    :data="item"
                    @close-tab="removeTab"
                ></WarningNoticeDetailStatic>
                <!-- 同步条目黑白名单 -->
                <WarningNoticeDetailBlackWhite
                    v-if="['addBW', 'lookBW', 'editBW'].includes(item.type)"
                    :data="item"
                    @close-tab="removeTab"
                ></WarningNoticeDetailBlackWhite>
                <!-- 系统运维(引擎) -->
                <WarningNoticeDetailEngine
                    v-if="['addEngine', 'lookEngine', 'editEngine'].includes(item.type)"
                    :data="item"
                    @close-tab="removeTab"
                ></WarningNoticeDetailEngine>
                <!-- 实时风险偏离度监控 -->
                <WarningNoticeDetailRiskDeviation
                    v-else-if="['addRD', 'lookRD', 'editRD'].includes(item.type)"
                    :data="item"
                    @close-tab="removeTab"
                ></WarningNoticeDetailRiskDeviation>
                <!-- 静态风险处置 -->
                <WarningNoticeDetailStaticRiskDisposal
                    v-else-if="['addStaticRiskDisposal', 'lookStaticRiskDisposal', 'editStaticRiskDisposal'].includes(item.type)"
                    :data="item"
                    @close-tab="removeTab"
                ></WarningNoticeDetailStaticRiskDisposal>
                <!-- OIS分项授信额度 -->
                <WarningNoticeDetailOIS v-else-if="['addOIS', 'lookOIS', 'editOIS'].includes(item.type)" :data="item" @close-tab="removeTab" />
                <!-- 历史记录 -->
                <EmailHistory v-if="item.type === 'history'" :data="item" @close-tab="removeTab" />
            </el-tab-pane>
        </el-tabs>
    </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import WarningNoticeList from './warning-notice/warning-notice-list.vue';
import WarningNoticeDetail from './warning-notice/warning-notice-detail.vue';
import WarningNoticeDetailStatic from './warning-notice/warning-notice-detail-static.vue';
import EmailHistory from './warning-notice/email-history.vue';
import WarningNoticeDetailBlackWhite from './warning-notice/warning-notice-detail-black-white.vue';
import WarningNoticeDetailEngine from './warning-notice/warning-notice-detail-engine.vue';
import WarningNoticeDetailRiskDeviation from './warning-notice/warning-notice-detail-risk-deviation.vue';
import WarningNoticeDetailOIS from './warning-notice/warning-notice-detail-OIS.vue';
import WarningNoticeDetailStaticRiskDisposal from './warning-notice/warning-notice-detail-static-risk-disposal.vue';
import EmailSendManage from './warning-notice/email-send-manage.vue';
import { getConfigItem } from '@/utils/tools';

@Component({
    name: 'WarningNoticeCmschina',
    components: {
        WarningNoticeList,
        WarningNoticeDetail,
        WarningNoticeDetailStatic,
        EmailHistory,
        WarningNoticeDetailBlackWhite,
        WarningNoticeDetailEngine,
        WarningNoticeDetailRiskDeviation,
        WarningNoticeDetailOIS,
        WarningNoticeDetailStaticRiskDisposal,
        EmailSendManage,
    },
})
// 区别产品版本只有资管样式
export default class WarningNoticeCmschina extends Vue {
    private activeKey: string = '触警邮件推送规则设置';
    private tabGroup: any[] = [];
    isExit3: boolean = false; // 是否存在资管的静态风险处置
    created() {
        this.$on('addTables', this.changComponets);
    }

    mounted() {
        const isExit3: boolean = getConfigItem('WEB_DOCKING') === 'CMSCHINA';
        this.isExit3 = isExit3;
    }

    // 监控子组件发送的命令
    changComponets(datas: any) {
        const data: any = { type: datas.type, content: datas.value };
        if (datas.type === 'add') {
            data.name = '新增风险监控屏邮件推送规则';
        } else if (datas.type === 'addStatic') {
            data.name = '新增静态风控邮件推送规则';
        } else if (datas.type === 'addBW') {
            data.name = '新增黑白名单邮件推送规则';
        } else if (datas.type === 'addEngine') {
            data.name = '新增系统运维邮件推送规则';
        } else if (datas.type === 'addRD') {
            data.name = '新增风险偏离监控邮件推送规则';
        } else if (datas.type === 'addOIS') {
            data.name = '新增分项授信额度邮件推送规则';
        } else if (datas.type === 'addStaticRiskDisposal') {
            data.name = '新增静态风险处置邮件推送规则';
        } else if (['edit', 'editStatic', 'editBW', 'editEngine', 'editRD', 'editOIS', 'editStaticRiskDisposal'].includes(datas.type)) {
            data.name = '修改邮件推送规则-' + datas.value.ruleId;
        } else if (['look', 'lookStatic', 'lookBW', 'lookEngine', 'lookRD', 'lookOIS', 'lookStaticRiskDisposal'].includes(datas.type)) {
            data.name = '查看邮件推送规则-' + datas.value.ruleId;
        } else if (datas.type === 'history') {
            data.name = '查看邮件推送记录-' + datas.value.ruleId;
        }
        const item = this.tabGroup.find((item: any) => item.name === data.name);
        if (!item) {
            this.addTab(data);
        } else {
            this.activeKey = data.name;
        }
    }

    private addTab(targetName: any) {
        this.tabGroup.push(targetName);
        this.activeKey = targetName.name;
    }

    // private closeTab(targetName) {

    // }

    private removeTab(targetName: string) {
        console.log(targetName, 'targetName1');

        let tabs = this.tabGroup;
        let activeName = this.activeKey;
        if (activeName === targetName) {
            tabs.forEach((tab: any, index: any) => {
                if (tab.name === targetName) {
                    let nextTab = tabs[index + 1] || tabs[index - 1];
                    if (nextTab) {
                        activeName = nextTab.name;
                    }
                }
            });
        }
        var str = targetName;
        str = str.substr(0, str.indexOf('：'));
        // 默认跳转
        this.activeKey = '触警邮件推送规则设置';
        this.tabGroup = tabs.filter((tab: any) => tab.name !== targetName);
    }
}
</script>
<style lang="scss" scoped>
@import '@/project/cmschina/style/cmschina.scss';
#notice {
    padding-top: 5px;
    min-width: 1200px;
    height: 100%;
}
#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item {
    line-height: 34px;
    padding: 0 25px;
}

#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item span {
    border-right: 0px solid #fff;
}
#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item:nth-child(1) .el-icon-close {
    display: none;
}
.close-tab-second /deep/ .el-tabs--top.el-tabs .el-tabs__nav .el-tabs__item:nth-child(2) .el-icon-close {
    display: none;
}

/deep/ .el-tabs {
    height: 100%;
    .el-tabs__header {
        min-width: auto;
        z-index: 111;
    }
    .el-tabs__content {
        height: calc(100% - 40px);
        .el-tab-pane {
            height: 100%;
        }
    }
}
</style>
